package com.shenghui.enterprise.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenghui.enterprise.base.entity.*;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.mapper.BaseRoleMapper;
import com.shenghui.enterprise.base.mapper.BaseRoleUserMapper;
import com.shenghui.enterprise.base.service.IBaseRoleService;
import com.shenghui.enterprise.base.service.IBaseRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Description: 角色用户关系表-role_user- 服务实现类
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-05-29
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@Service
public class BaseRoleUserServiceImpl extends ServiceImpl<BaseRoleUserMapper, BaseRoleUser> implements IBaseRoleUserService {

    @Autowired
    private EntityCommon entityCommon;
    @Autowired
    private BaseRoleUserMapper baseRoleUserMapper;
    @Autowired
    private BaseRoleMapper roleMapper;
    @Autowired
    private IBaseRoleService baseRoleService;

    @Override
    public Object getAuditRoleTree(String userId, String id, String code) {
        //获取当前登录用户
        if (!StringUtils.isEmpty(userId)) {
            //获取当前登录用户有权限的所有模块信息
            QueryWrapper query = new QueryWrapper<BaseRole>();
            query.select("role_id", "name");

            List<Map> roleList = roleMapper.selectMaps(query.orderBy(true, false, "order_value"));
            if (roleList != null && roleList.size() > 0) {
                for (Map map_m : roleList) {
                    //获取模块是不是有子集
                    query = new QueryWrapper<BaseRole>();
                    query.select("role_id", "name");
                    map_m.put("isParent", false);

                    //获取当前授权角色有权限的模块id信息
                    query = new QueryWrapper<BaseRoleUser>();
                    query.eq("role_id",  map_m.get("roleId"));
                    query.eq("user_id", userId);

                    BaseRoleUser baseRoleUser = baseRoleUserMapper.selectOne(query);
                    if (!StringUtils.isEmpty(baseRoleUser)) {
                        map_m.put("checked", true);
                    } else {
                        map_m.put("checked", false);
                    }
                    map_m.put("name", map_m.get("name"));
                    map_m.put("id", map_m.get("roleId"));
                }
            }
            return roleList;
        }
        return null;
    }

    @Override
    public Object getRoleUserListByUserId(Map map) {
        Page<Map> mapPage = new Page<Map>(map);
        List<Map> list = baseRoleUserMapper.getRoleUserListByUserId(mapPage, map);
        return new PageModel(mapPage.getTotal(),list);
    }

    @Override
    @Transactional
    public Result addRoleUser(String roleId, String userId) {
        if (GeneralUtil.isNull(roleId)) {
            return Result.error("roleId不能为空");
        }
        if (GeneralUtil.isNull(userId)) {
            return Result.error("userId不能为空");
        }

        List<String> idList = new ArrayList<>();
        // 获取所有角色ID
        List<String> allRoleIds = baseRoleService.getChildRoleIdByParentId(roleId, idList);
        for (String rId: allRoleIds) {
            BaseRoleUser baseRoleUser = new BaseRoleUser();
            baseRoleUser.setRoleId(rId);
            baseRoleUser.setUserId(userId);
            entityCommon.createBase(baseRoleUser);
            baseRoleUserMapper.insert(baseRoleUser);
        }
        return Result.ok("授权成功!");
    }

    @Override
    @Transactional
    public Result removeRoleUser(String roleId, String userId) {
        if (GeneralUtil.isNull(roleId)) {
            return Result.error("roleId不能为空");
        }
        if (GeneralUtil.isNull(userId)) {
            return Result.error("userId不能为空");
        }

        List<String> idList = new ArrayList<>();
        // 获取所有角色ID
        List<String> allRoleIds = baseRoleService.getChildRoleIdByParentId(roleId, idList);
        for (String rId: allRoleIds) {
            Map<String, Object> delParams = new HashMap<>();
            delParams.put("role_id", rId);
            delParams.put("user_id", userId);
            baseRoleUserMapper.deleteByMap(delParams);
        }
        return Result.ok("取消成功!");
    }

}
